package com.kehutong.house.service;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.house.entity.House;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.util.Objects;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@WebService("/basic/building/service")
public class BasicService {

	@Inject
	private DoveClient doveClient;

	/**
	 * 添加操作日志
	 * @return
	 * @throws Exception
	 */
	public void saveOperateRecord(Token token, String businessId, String businessType, String operateType, String desc) throws Exception {
		JSONObject body = new JSONObject();
		body.put("businessId", businessId);
		body.put("businessType", businessType);
		body.put("operateName", token.getUsername());
		body.put("operateDesc", desc);
		body.put("operateNo", token.getUuid());
		body.put("operateType", operateType);
		doveClient.post("/basic/operate/record/service/save", http->{
			http.setBody(body.toString());
		});
	}

    /**
     * 添加归属记录日志
     * @return
     * @throws Exception
     */
    public void saveAttributeRecord(Token token, JSONObject jsonObject) throws Exception {
        JSONObject body = new JSONObject();
        body.put("businessId", jsonObject.getString("businessId"));
        body.put("businessType", jsonObject.getString("businessType"));
        body.put("operateName", token.getUsername());
        body.put("operateNo", token.getUuid());
        body.put("beforeNo", jsonObject.getString("beforeNo"));
        body.put("currentNo", jsonObject.getString("currentNo"));
        body.put("beforeNos", jsonObject.getJSONArray("beforeNos"));
        body.put("currentNos", jsonObject.getJSONArray("currentNos"));
        doveClient.post("/basic/attribute/record/service/save", http->{
            http.setBody(body.toString());
        });
    }

	/**
	 * 添加归属记录日志
	 * @return
	 * @throws Exception
	 */
	public void saveAttributeRecord(JSONObject jsonObject) throws Exception {
		JSONObject body = new JSONObject();
		body.put("businessId", jsonObject.getString("businessId"));
		body.put("businessType", jsonObject.getString("businessType"));
		body.put("operateName", jsonObject.getString("operateName"));
		body.put("operateNo", jsonObject.getString("operateNo"));
		body.put("beforeNo", jsonObject.getString("beforeNo"));
		body.put("beforeName", jsonObject.getString("beforeName"));
		body.put("currentNo", jsonObject.getString("currentNo"));
		body.put("currentName", jsonObject.getString("currentName"));
		body.put("startTime", jsonObject.getString("startTime"));

		doveClient.post("/basic/attribute/record/service/save", http->{
			http.setBody(body.toJSONString());
		});
	}

	public void updateAttributeRecord(JSONObject jsonObject) throws Exception {
		doveClient.post("/basic/attribute/record/service/update", http->{
			http.setBody(jsonObject.toJSONString());
		});
	}

	public JSONObject getAttributeRecord(JSONObject jsonObject) throws Exception {
		JSONObject body = new JSONObject();
		body.put("businessId", jsonObject.getString("businessId"));
		body.put("businessType", jsonObject.getString("businessType"));
		JSONObject result = doveClient.post("/basic/attribute/record/service/get", http->{
			http.setBody(body.toJSONString());
		});
		if (Objects.isNull(result) || Objects.isNull(result.getJSONObject("data"))) {
			return null;
		}

		return result.getJSONObject("data");
	}
}
